#!/usr/bin/env python3
# -*- coding:utf-8 -*- 
from PyQt5 import QtCore, QtGui
import sys
from PyQt5.QtCore import QEventLoop, QTimer
from PyQt5.QtWidgets import QApplication, QMainWindow
import time
  
from PyQt5 import QtCore, QtWidgets
  

#!/usr/bin/env python3
# -*- coding:utf-8 -*- 
from PyQt5 import QtCore, QtGui
import sys
from PyQt5.QtCore import QEventLoop, QTimer
from PyQt5.QtWidgets import QApplication, QMainWindow
  
from Ui_ControlBoard import Ui_MainWindow
  
class EmittingStr(QtCore.QObject):
    textWritten = QtCore.pyqtSignal(str) #定义一个发送str的信号
    def write(self, text):
      self.textWritten.emit(str(text))
  
  
class ControlBoard(QMainWindow):
  my_signal = QtCore.pyqtSignal(int)
  def __init__(self):
    super(ControlBoard, self).__init__()
    self.setupUi(self)
    # 下面将输出重定向到textBrowser中
    sys.stdout = EmittingStr(textWritten=self.outputWritten)
    sys.stderr = EmittingStr(textWritten=self.outputWritten)
    self.my_signal.connect(self.printABCD)			#信号连接到printABCD
    self.time_up()					#开启循环条件
  
  def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(800, 253)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.pushButton = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton.setGeometry(QtCore.QRect(640, 140, 75, 23))
    self.pushButton.setObjectName("pushButton")
    self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
    self.textBrowser.setGeometry(QtCore.QRect(170, 0, 256, 192))
    self.textBrowser.setObjectName("textBrowser")
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtWidgets.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
    self.menubar.setObjectName("menubar")
    MainWindow.setMenuBar(self.menubar)
    self.statusbar = QtWidgets.QStatusBar(MainWindow)
    self.statusbar.setObjectName("statusbar")
    MainWindow.setStatusBar(self.statusbar)

    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

  def retranslateUi(self, MainWindow):
      _translate = QtCore.QCoreApplication.translate
      MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
      self.pushButton.setText(_translate("MainWindow", "确定"))

  def outputWritten(self, text):
    cursor = self.textBrowser.textCursor()
    cursor.movePosition(QtGui.QTextCursor.End)
    cursor.insertText(text)
    self.textBrowser.setTextCursor(cursor)
    self.textBrowser.ensureCursorVisible()
  
  def time_up(self):					#写了个循环条件触发信号
    a = 0
    while a < 10:
      a = a +1
      if a >5:
        self.my_signal.emit(a)
        print(a)

  def bClicked(self):
    """Runs the main function."""
    print('Begin')
    print("End")
  
  def printABCD(self,b):				#接收信号
      if b == 6:
        print("YES")
      else:
        print("NO")
  
if __name__ == "__main__":
  app = QApplication(sys.argv)
  win = ControlBoard()

  win.show()
  sys.exit(app.exec_())


